#!/bin/bash
set -e

source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/../common.sh"

#下载依赖和工具包
function install_development() {
  sudo yum install -y wget tar unzip patch perl-GD perl-CPAN
  cpan GD::Text::Align
  cpan PerlIO::gzip
  source /etc/profile
}

#获取GD_Graph_bars
function download_GD_Graph_bars() {
  echo "# GD_Graph_bars正在拷贝"
  cp  "$2"/src/GDGraph.zip  "$1" 
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# GD_Graph_bars拷贝成功\033[0m"
  else
    echo -e "\033[31m# GD_Graph_bars拷贝失败,请重新执行脚本\033[0m"
    exit 1
  fi
}

function install_GD_Graph_bars() {
  echo "# GD_Graph_bars正在安装"
  unzip "$1"/GDGraph.zip -d "$1"
  cd "$1"/GDGraph-1.48/
  perl Makefile.PL 
  echo -e "\033[1;32;1m# 开始执行sed替换gcc为clang\033[0m"
  sed -i "s/gcc/clang/g" `grep gcc -rl Makefile`
  echo -e "\033[1;32;1m# 结束执行sed替换gcc为clang\033[0m"
  make  && make  install
  echo -e "\033[1;32;1m# GD_Graph_bars已经安装完毕\033[0m"
}



function main() {
    is_check_build_install_directory "$1" "$2" "GD_Graph_bars.sh"
    is_equal_build_install_path "$1" "$2"
    install_development
    download_GD_Graph_bars "$1" "$4"
    install_GD_Graph_bars "$1" "$2" "$3"
}

main "$@"